package minicad;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;


/**
 * MiniCAD
 */
public class MiniCAD {
    JFrame frame = new JFrame("MiniCAD");
    JPanel panel = new JPanel();
    JPanel interactPanel = new JPanel();

    JPanel drawButtonPanel = new JPanel();
    String[] drawButtonNames = 
        new String[]{"Line","Rect","Circ","Text"};
    JButton[] drawButtons = new JButton[drawButtonNames.length];
    Map<String,JButton> drawButtonMap =new HashMap<>();

    JPanel controlButtonPanel = new JPanel();
    String[] controlButtonNames = 
        new String[]{"Select", "Scale up", "Scale down"};
    JButton[] controlButtons = new JButton[controlButtonNames.length];
    Map<String,JButton> controlButtonMap =new HashMap<>();
    
    JTextField textField = new JTextField("Select FIRST and input color(e.g blue) here!");

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    JMenuItem saveItem = new JMenuItem("Save");

    public static void main(String[] args) {
        MiniCAD gui = new MiniCAD();
        gui.run();
    }

    void run() {
        // Main frame setting
        setMainFrame();
        // drawing panel
        setMainPanel();
        // buttons; we have drawButtons and controlButtons
        setDrawButtonPanel();
        setControlButtonPanel();

        // interatPanel, include buttons and text input
        setInteractPanel();

        // memus for open and save
        setMenu();
        // file dialog for open and save
        FileDialog openDiag = new FileDialog(frame,"Open File", FileDialog.LOAD);
        FileDialog saveDiag = new FileDialog(frame,"Save File", FileDialog.SAVE);

        Viewer viewer = new Viewer(panel);
        Model model = new Model(viewer);
        Controller controller = new Controller(model);
        controller.addStateControl(controlButtonMap.get("Select"));
        controller.addDrawStateControl(drawButtons);
        controller.addDrawControl(frame,panel);
        controller.addSelectControl(panel);
        controller.addModControl(textField,controlButtonMap.get("Scale up"),controlButtonMap.get("Scale down"));        
        controller.addFileControl(openItem, saveItem, openDiag, saveDiag);


        frame.setVisible(true);
    }

    void setMainFrame() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 700);
    }
    void setMainPanel(){
        frame.getContentPane().add(BorderLayout.CENTER,panel);
    }
    void setInteractPanel(){
        frame.getContentPane().add(BorderLayout.EAST,interactPanel);
        interactPanel.setLayout(new GridLayout(4,1));
        interactPanel.add(drawButtonPanel);
        interactPanel.add(controlButtonPanel);
        interactPanel.add(textField);

    }
    void setDrawButtonPanel(){
        for(int i = 0;i<drawButtonNames.length;++i) {
            drawButtons[i] = new JButton(drawButtonNames[i]);
            drawButtonMap.put(drawButtonNames[i], drawButtons[i]);
            drawButtonPanel.add(drawButtons[i]);
        }

    }
    void setControlButtonPanel(){
        for(int i = 0;i<controlButtonNames.length;++i) {
            controlButtons[i] = new JButton(controlButtonNames[i]);
            controlButtonMap.put(controlButtonNames[i], controlButtons[i]);
            controlButtonPanel.add(controlButtons[i]);
        }
    }
    void setMenu(){
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);
    }
}
